from typing import List
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from app.api import deps
from app.crud import technology_tag as crud_technology_tag
from app.schemas.technology_tag import (
    TechnologyTagCreate,
    TechnologyTagUpdate,
    TechnologyTagInDB,
    TechnologyTagList,
    TechnologyTagFilter
)

router = APIRouter()

@router.get("/all", response_model=List[TechnologyTagInDB])
def get_all_technology_tags(
    db: Session = Depends(deps.get_db)
):
    """
    获取所有技术方向标签，不分页
    """
    return crud_technology_tag.get_all_technology_tags(db)

@router.get("/", response_model=TechnologyTagList)
def get_technology_tags(
    db: Session = Depends(deps.get_db),
    name: str = None,
    search: str = None,
    page: int = 1,
    page_size: int = 10
):
    """
    获取技术方向标签列表，支持分页和搜索
    """
    filter_params = TechnologyTagFilter(
        name=name,
        search=search,
        page=page,
        page_size=page_size
    )
    tags, total = crud_technology_tag.get_technology_tags(db, filter_params)
    return {
        "total": total,
        "items": tags
    }

@router.post("/", response_model=TechnologyTagInDB)
def create_technology_tag(
    *,
    db: Session = Depends(deps.get_db),
    tag_in: TechnologyTagCreate
):
    """
    创建新的技术方向标签
    """
    # 检查标签名是否已存在
    if crud_technology_tag.get_technology_tag_by_name(db, tag_in.name):
        raise HTTPException(
            status_code=400,
            detail="Tag with this name already exists"
        )
    tag = crud_technology_tag.create_technology_tag(db, tag_in)
    return tag

@router.get("/{tag_id}", response_model=TechnologyTagInDB)
def get_technology_tag(
    tag_id: int,
    db: Session = Depends(deps.get_db)
):
    """
    获取指定技术方向标签详情
    """
    tag = crud_technology_tag.get_technology_tag(db, tag_id)
    if not tag:
        raise HTTPException(status_code=404, detail="Technology tag not found")
    return tag

@router.put("/{tag_id}", response_model=TechnologyTagInDB)
def update_technology_tag(
    *,
    db: Session = Depends(deps.get_db),
    tag_id: int,
    tag_in: TechnologyTagUpdate
):
    """
    更新技术方向标签
    """
    # 如果要更新名称，检查新名称是否已存在
    if tag_in.name:
        existing_tag = crud_technology_tag.get_technology_tag_by_name(db, tag_in.name)
        if existing_tag and existing_tag.id != tag_id:
            raise HTTPException(
                status_code=400,
                detail="Tag with this name already exists"
            )
    
    tag = crud_technology_tag.update_technology_tag(db, tag_id, tag_in)
    if not tag:
        raise HTTPException(status_code=404, detail="Technology tag not found")
    return tag

@router.delete("/{tag_id}")
def delete_technology_tag(
    *,
    db: Session = Depends(deps.get_db),
    tag_id: int
):
    """
    删除技术方向标签
    """
    success = crud_technology_tag.delete_technology_tag(db, tag_id)
    if not success:
        raise HTTPException(status_code=404, detail="Technology tag not found")
    return {"status": "success"} 